package com.example.activitytest

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity

class FirstActivity : AppCompatActivity() {

    // Create ActivityResultLauncher
    private val launcher =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { activityResult ->
            Toast.makeText(this, "FirstActivity ${activityResult.toString()}", Toast.LENGTH_SHORT)
                .show()
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.first_layout)
        val button1 = findViewById<Button>(R.id.button1)
        button1.setOnClickListener(){
            Toast.makeText(this, "click button 1", Toast.LENGTH_SHORT).show();
//            val intent: Intent = Intent(this, SecondActivity::class.java)
//            startActivity(intent)

//            val intent = Intent("com.example.activitytest.ACTION_START")
//            startActivity(intent)

//            val intent = Intent(Intent.ACTION_VIEW)
//            intent.data = Uri.parse("https://www.baidu.com")
//            startActivity(intent)

//            val dataStr = "Hello"
//            val dataInt = 25
//            val intent: Intent = Intent(this, SecondActivity::class.java)
//            intent.putExtra("str", dataStr)
//            intent.putExtra("int", dataInt)
//            startActivity(intent)

            val intent = Intent(this, SecondActivity::class.java)
            launcher.launch(intent)
        }
    }


    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main, menu)
        return true;
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.add_item-> Toast.makeText(this, "add_item", Toast.LENGTH_SHORT).show()
            R.id.remove_item -> Toast.makeText(this, "remove_item", Toast.LENGTH_SHORT).show()
        }
        return true;
    }
}